<?php
/**
 * Created by PhpStorm.
 * User: jianhe
 * Date: 2018/1/23
 * Time: 14:00
 */
namespace App\Services\Cart;

/**
 *购物车商品
 */
class CartGoodsService
{
    
    public function __construct($goods)
    {
        $this->rec_id=$goods['rec_id'];
        $this->goods_id=$goods['goods_id'];
        $this->brand_id=$goods['brand_id'];
        $this->cat_id=$goods['cat_id'];
        $this->goods_sn=$goods['goods_sn'];
        $this->goods_name=$goods['goods_name'];
        $this->goods_price=$goods['goods_price'];
        $this->vip_price=$goods['vip_price'];
        $this->current_number=$goods['num'];
        $this->current_price=$goods['goods_price'];
        $this->giving=$goods['giving'];
        $this->market_price=$goods['market_price'];
        $this->is_real=$goods['is_real'];
        $this->goods_weight=$goods['goods_weight'];
        $this->spec_value=$goods['spec_value'];
        $this->spec_unit=$goods['spec_unit'];
        $this->goods_stock=$goods['goods_stock'];
        if(!isset($goods['add_price'])){
            $goods['add_price']=$goods['goods_price'];
        }
        $this->add_price=$goods['add_price'];
        $this->delivery_type=$goods['delivery_type'];
        $this->delivery_cycle=$goods['delivery_cycle'];
        $this->min_buy_num=$goods['min_buy_num'];
        $this->goods_thumb=$goods['goods_thumb'];
        $this->goods_img=$goods['goods_img'];
        $this->goods_brief=$goods['goods_brief'];
        $this->extension_code=$goods['extension_code'];
        $this->is_auto_morrow_presell=$goods['is_auto_morrow_presell'];
        $this->is_buynow=$goods['is_buynow'];
        $this->is_presell=$goods['is_presell'];
        if(isset($goods['expiration_date'])){
            $this->expiration_date=$goods['expiration_date'];
        }
        $this->old_goods_price=$goods['old_goods_price'];
        
    }

    /**
     * @var void 商品信息
     */
    private $rec_id;
    private $goods_id;
    private $goods_sn;
    private $goods_name;
    private $goods_price;
    private $vip_price;
    private $cat_id;
    private $brand_id;
    private $user_id;
    private $isPromote=0;
    private $market_price=0;
    private $is_real=1;
    private $goods_weight;
    private $spec_value;
    private $spec_unit;
    private $goods_stock;
    private $add_price;
    private $delivery_type;
    private $delivery_cycle;
    private $min_buy_num;
    private $goods_thumb;
    private $goods_img;
    private $goods_brief;
    private $extension_code;
    private $is_auto_morrow_presell;
    private $is_buynow;
    private $is_presell;
    private $expiration_date=0;
    private $wid;
    private $old_goods_price;

    /**
     * @author Jianhe
     * @return mixed
     */
    public function getOldGoodsPrice()
    {
        return $this->old_goods_price;
    }


    /**
     * @var void 当前价格
     */
    private $current_price;

    /**
     * @var void 当前数量
     */
    private $current_number;

    /**
     * @var void 商品类型
     */
    private $giving;

    /**
     * @var void 是否有选中
     */
    private $is_check=1;

    /**
     * @var void 可参与的活动
     */
    private $eligibleActArray;

    /**
     * @var void 当前参与的活动
     */
    private $currentAct;
    
    

    /**
     * @author Jianhe
     * @return void
     */
    public function getRecId()
    {
        return $this->rec_id;
    }
    
    

    /**
     * @author Jianhe
     * @param void $rec_id
     */
    public function setRecId($rec_id)
    {
        $this->rec_id = $rec_id;
    }
    
    

    /**
     * @author Jianhe
     * @return mixed
     */
    public function getGoodsId()
    {
        return $this->goods_id;
    }

    /**
     * @author Jianhe
     * @return mixed
     */
    public function getCatId()
    {
        return $this->cat_id;
    }

    /**
     * @author Jianhe
     * @return mixed
     */
    public function getBrandId()
    {
        return $this->brand_id;
    }

    
    
    /**
     * @author Jianhe
     * @param void $eligibleActArray
     */
    public function setEligibleActArray($eligibleActArray)
    {
        $this->eligibleActArray = $eligibleActArray;
    }
    
    

    /**
     * 增加数量
     */
    public function increase($num)
    {
        if($num<1){
            return false;
        }
        $this->current_number+=$num;
        return $this->current_number;
    }

    /**
     * 减少数量
     */
    public function reduce($num)
    {
        if($this->current_number<$num){
            return false;
        }
        $this->current_number-=$num;
        return $this->current_number;
    }

    /**
     * @return void
     */
    public function getCurrentAct()
    {
        return $this->currentAct;
    }

    /**
     * @author Jianhe
     * @param void $currentAct
     */
    public function setCurrentAct($currentAct)
    {
        $this->currentAct = $currentAct;
    }

    /**
     * @return void
     */
    public function getEligibleActArray()
    {
        return $this->eligibleActArray;
    }

    /**
     * @role 获取商品类型
     * @author Jianhe
     */
    public function getGivingTag(){
        $giving=$this->giving;
        //促销
        if($this->isPromote==1){
            $giving=4;
        }
        //秒杀
        if($this->is_buynow==1){
            $giving=5;
        }
        //充值赠送
        return $giving;
    }

    /**
     * @role 判断价格是否变动
     * @author Jianhe
     * @return bool
     */
    public function price_change(){
        if($this->current_price==$this->old_goods_price || empty($this->old_goods_price)){
            return false;
        }
        return true;
    }

    /**
     * @role 格式化商品-输出信息-提交订单用
     * @author Jianhe
     * @param $proportion
     * @return array
     */
    public function getGoodsForOrderFormat($proportion){
        $goods=array();
        $goods['bonus_price']=sprintf("%.2f",$proportion*$this->current_price);
        $goods['giving']=$this->getGivingTag();
        $goods['goods_attr']="";
        $goods['goods_attr_id']="";
        $goods['discount_price']=0;//....
        $goods['goods_discount']=0;//........
        $goods['goods_id']=$this->goods_id;
        $goods['goods_name']=$this->goods_name;
        $goods['goods_number']=$this->current_number;
        $goods['goods_price']=$this->current_price-$goods['bonus_price'];
        $goods['before_bonus_goods_price']=$this->current_price;
        $goods['goods_sn']=$this->goods_sn;
        $goods['is_cashier']="";
        $goods['is_gift']=$this->giving==1?1:0;
        $goods['is_presell']=$this->is_presell;
        $goods['is_quota']="0";
        $goods['is_real']=$this->is_real;
        $goods['market_price']=$this->market_price;
        $goods['parent_id']=0;
        $goods['product_id']=$this->goods_id;
        $goods['rec_id']=$this->rec_id;
        $goods['extension_code']=$this->extension_code;
        $goods['price_change']=$this->price_change()?1:1;
        return $goods;
    }

    /**
     * @role 格式化商品-输出信息
     * @author Jianhe
     */
    public function getGoodsFormat(){
        $activityList=array();
        if(!empty($this->eligibleActArray)){
            foreach($this->eligibleActArray as $activity){
                $activityList[]=$activity->getActivityFormat();
            }
        }

        $goods=array();
        $goods['act_id']=is_object($this->currentAct)?$this->currentAct->getActId():0;
        $goods['activity_list']=$activityList;
        $goods['bonus_price']=0;
        $goods['brand_id']=0;
        $goods['can_handsel']=0;
        $goods['cashier_id']="";
        $goods['cat_id']=$this->cat_id;
        $goods['delivery_cycle']=$this->delivery_cycle;
        $goods['delivery_type']=$this->delivery_type;
        $goods['expiration_date']=$this->expiration_date;
        $goods['extension_code']=$this->extension_code;
        $goods['giving']=$this->getGivingTag();
        $goods['goods_attr']="";
        $goods['goods_attr_id']="";
        $goods['goods_brief']=$this->goods_brief;
        $goods['goods_discount']=0;//........
        $goods['goods_id']=$this->goods_id;
        $goods['goods_img']=$this->goods_img;
        $goods['goods_name']=$this->goods_name;
        $goods['goods_number']=$this->current_number;
        $goods['goods_price']=$this->current_price;
        $goods['goods_sn']=$this->goods_sn;
        $goods['goods_stock']=$this->goods_stock;
        $goods['goods_thumb']=$this->goods_thumb;
        $goods['goods_weight']=$this->goods_weight;
        $goods['had_reduced_price']=$this->current_price>$this->add_price;
        $goods['is_auto_morrow_presell']=$this->is_auto_morrow_presell;
        $goods['is_buynow']=(string)$this->is_buynow;
        $goods['is_cashier']="";
        $goods['is_check']=(string)$this->is_check;
        $goods['is_delete']="";
        $goods['is_gift']=$this->giving==1?1:0;
        $goods['is_on_sale']=(string)1;
        $goods['is_overdue']=$this->expiration_date!=0 && $this->expiration_date<gm_time()?1:0;
        $goods['is_presell']=$this->is_presell;
        $goods['is_quota']="0";
        $goods['is_real']=$this->is_real;
        $goods['is_shipping']="0";
        $goods['market_price']=$this->market_price;
        $goods['min_buy_num']=$this->min_buy_num;
        $goods['muti_acivity']=!empty($this->eligibleActArray)?1:0;
        $goods['overdue_date']=$this->expiration_date!=0 && $this->expiration_date+86400<gm_time()?gmdate("Y-m-d",$this->expiration_date):"";
        $goods['parent_id']=0;
        $goods['pid']=$this->goods_id;
        $goods['practical_num']=$this->goods_stock;
        $goods['preselltimeres']=0;//........
        $goods['product_id']=$this->goods_id;
        $goods['promote_end_date']=0;//........
        $goods['promote_goods_number']=0;//........
        $goods['promote_price']=0;//........
        $goods['promote_start_date']=0;//........
        $goods['promote_today_number']=0;//........
        $goods['promote_user_rank']=0;//........
        $goods['rec_id']=(string)$this->rec_id;
        $goods['rec_type']="";
        $goods['reduced_price']=$this->current_price-$this->add_price;
        $goods['session_id']="";
        $goods['shop_price']=$this->goods_price;
        $goods['spec_unit']=$this->spec_unit;
        $goods['spec_value']=$this->spec_value;
        $goods['subtotal']=price_format($this->current_price*$this->current_number);
        $goods['true_vip_price']=$this->vip_price;
        $goods['user_id']=$this->user_id;
        $goods['vip_price']=$this->vip_price;
        $goods['wid']=$this->wid;
        return $goods;
    }

    /**
     * @author Jianhe
     * @return mixed
     */
    public function getGoodsStock()
    {
        return $this->goods_stock;
    }

    /**
     * @author Jianhe
     * @return void
     */
    public function getCurrentNumber()
    {
        return $this->current_number;
    }

    /**
     * @author Jianhe
     * @return void
     */
    public function getIsCheck()
    {
        return $this->is_check;
    }

    /**
     * @author Jianhe
     * @param void $is_check
     */
    public function setIsCheck($is_check)
    {
        $this->is_check = $is_check;
    }
    
    

    /**
     * @author Jianhe
     * @return void
     */
    public function getCurrentPrice()
    {
        return $this->current_price;
    }

    /**
     * @author Jianhe
     * @return int
     */
    public function getMarketPrice()
    {
        return $this->market_price;
    }

    /**
     * @author Jianhe
     * @return mixed
     */
    public function getVipPrice()
    {
        return $this->vip_price;
    }

    /**
     * @author Jianhe
     * @return mixed
     */
    public function getGoodsPrice()
    {
        return $this->goods_price;
    }

    /**
     * @author Jianhe
     * @return int
     */
    public function getIsReal()
    {
        return $this->is_real;
    }

    /**
     * @author Jianhe
     * @return mixed
     */
    public function getAddPrice()
    {
        return $this->add_price;
    }

    /**
     * @author Jianhe
     * @return void
     */
    public function getGiving()
    {
        return $this->giving;
    }

    /**
     * @author Jianhe
     * @param void $current_number
     */
    public function setCurrentNumber($current_number)
    {
        $this->current_number = $current_number;
    }
    
    
    
    
    
    


    
    
    
    
    
    
    
    
    
    
    

    
    
    
    
    
}
